package com.net.core.config;

import com.fasterxml.jackson.databind.exc.InvalidFormatException;
import com.net.core.core.YXException;
import com.net.core.core.reuslt.GeneratorResult;
import com.net.core.core.reuslt.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
public class ExceptionController {

    @ExceptionHandler(YXException.class)
    @ResponseBody
    public Response IncorrectCredentialsException(YXException e) {
        log.error(e.getMessage(), e.getException());
        return GeneratorResult.genFailMessage(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Response IncorrectCredentialsException(Exception e) {
        log.error(e.getMessage(), e);
        Response<Object> objectResult = GeneratorResult.genFailMessage(e.toString());
        return objectResult;
    }

    @ExceptionHandler(InvalidFormatException.class)
    @ResponseBody
    public Response IncorrectCredentialsException(InvalidFormatException e) {
        log.error(e.getMessage(), e);
        Response<Object> objectResult = GeneratorResult.genFailMessage(e.toString());
        return objectResult;
    }

}
